/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.io.File;
import java.util.Date;

import net.bioteam.appweb.Config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ggf.drmaa.DrmaaException;
import org.ggf.drmaa.ExitTimeoutException;
import org.ggf.drmaa.JobInfo;
import org.ggf.drmaa.Session;

public class DRMAAStatusChecker extends UnixOSStatusChecker {

	public static final Log logger=LogFactory.getLog(DRMAAStatusChecker.class);
	public void checkStatus(UnfinishedJob status)  {
		String jobid = status.getJobid();

		File file = new File(status.getWorkdir() + File.separator
				+ UinxOSSubmiter.FINISH_MARK);
		int statuscode;
		try {
			statuscode = Config.drmmasession.getJobProgramStatus(jobid);

			switch (statuscode) {
			case Session.UNDETERMINED:
				logger.info(status.getId() +" job is set to unknown");
				status.setCode(RunStatusCode.UNKNOWN);
				status.setNote("DRM reported undetermined job status");
				status.setFinishedTime(new Date());
				break;
			case Session.DONE:
			case Session.FAILED:
				JobInfo info = Config.drmmasession.wait(jobid, 0);
				if (info.hasExited()) {
					logger.info(status.getId() +" job is set to finished");
					status.setCode(RunStatusCode.FINISHED);
					status.setExitValue(info.getExitStatus());
					status.setNote("DRM reported failed or finished");
				} else {
					logger.info(status.getId() +" job is set to aborted");
					status.setCode(RunStatusCode.ABORTED);
					status.setNote("DRM reported aborted");
					status.setExitValue(-1);
				}
				status.setFinishedTime(new Date());
				status.setFinishedTime(new Date(file.lastModified()));
				return;
			case Session.RUNNING:
				status.setCode(RunStatusCode.RUNNING);
				logger.info(status.getId() +" job is running");
				break;
			// case Session.QUEUED_ACTIVE:
			// case Session.SYSTEM_ON_HOLD:
			// case Session.SYSTEM_SUSPENDED:
			// case Session.USER_ON_HOLD:
			// case Session.USER_SUSPENDED:
			// case Session.USER_SYSTEM_ON_HOLD:
			// case Session.USER_SYSTEM_SUSPENDED:
			default:
				logger.info(status.getId() +" status by sge is "+statuscode);
				status.setCode(RunStatusCode.SUBMITTED);
				status.setNote("DRM reported holding or suspended");
				break;
			}
		} catch (org.ggf.drmaa.InvalidJobException e) {
			
			logger.warn(status.getId()+" InvalidJob: "+e.getMessage());
			// should not occur.
			// This only case that could occur is that the system is restarted.
			// For this job, we revert to file checker
			status.setCode(RunStatusCode.ABORTED);
			status.setNote("DRM can not find the specified job");
			status.setExitValue(-1);
			status.setFinishedTime(new Date());
			status.setFinishedTime(new Date(file.lastModified()));
			//super.checkStatus(status);

		} catch (ExitTimeoutException ie) {
			return;
		} catch (DrmaaException e) {
			logger.warn(e.getMessage());
			super.checkStatus(status);
			//throw e;
		}
	}

}
